package com.lvsc.sdaplacefinder.services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.hibernate.criterion.Restrictions;

import com.lvsc.sdaplacefinder.dao.DataServiceFactory;
import com.lvsc.sdaplacefinder.dao.SodaQuery;
import com.lvsc.sdaplacefinder.models.Place;

@Path("places")
public class PlacesRessource {

	@Context
	UriInfo uriInfo;
	@Context
	Request request;

	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getPlaces() {
		List<Place> places = new ArrayList<Place>();
		places.addAll(DataServiceFactory.getInstance().findAll(Place.class));
		GenericEntity<List<Place>> entity = new GenericEntity<List<Place>>(
				places) {
		};
		Response returnResponse = Response.ok().entity(entity).build();
		return returnResponse;
	}

	@GET
	@Path("cat/{cat}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getPlacesByCategory(@PathParam("cat") String cat) {
		List<Place> places = new ArrayList<Place>();

		SodaQuery<Place> q = DataServiceFactory.getInstance().getQuery(
				Place.class);
		q.add(Restrictions.like("category", cat));
		places.addAll(DataServiceFactory.getInstance().find(q));

		GenericEntity<List<Place>> entity = new GenericEntity<List<Place>>(
				places) {
		};
		Response returnResponse = Response.ok().entity(entity).build();
		return returnResponse;
	}

	@POST
	@Produces({ MediaType.APPLICATION_JSON })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response newPlace(@FormParam("name") String name,
			@FormParam("desc") String desc, @FormParam("lati") String latitude,
			@FormParam("longi") String longitude,
			@FormParam("category") String category,
			@Context HttpServletResponse servletResponse) throws IOException {

		Place place = new Place(name, desc, Double.parseDouble(latitude),
				Double.parseDouble(longitude), category);
		place.setCreated(new Date());

		DataServiceFactory.getInstance().persist(place);

		Response returnResponse = Response.created(uriInfo.getAbsolutePath())
				.entity(place).build();
		return returnResponse;
	}

	@Path("{place}")
	public PlaceResource getPlace(@PathParam("place") String id) {
		return new PlaceResource(uriInfo, request, id);
	}

	@GET
	@Path("rate/{place}/{rating}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response ratePlace(@PathParam("place") long placeId,
			@PathParam("rating") int rating) {
		Place place = DataServiceFactory.getInstance().getById(placeId,
				Place.class);
		Float oldRating = place.getRating();
		Integer nbRate = place.getNbRate();

		Float newRating = ((oldRating * nbRate) + rating) / (nbRate + 1);

		place.setRating(newRating);
		place.setNbRate(nbRate + 1);

		place = DataServiceFactory.getInstance().merge(place);

		Response returnResponse = Response.ok().entity(place).build();
		return returnResponse;
	}
}
